<template>
  <div style="padding-top: 24px">
    <fm-generate-form
      ref="generateForm"
      v-loading="loading"
      :data="formConfig"
      :value="formValue"
      :edit="edit"
      @on-change="formChange"
    />

    <div style="text-align: center">
      <el-button v-if="!edit" type="primary" :loading="loading" @click="submit">提交</el-button>
      <el-button v-if="!edit" @click="reset">重置</el-button>
      <el-button @click="back">返回</el-button>
    </div>

    <send-flow-modal
      v-if="isShowModal"
      :is-show.sync="isShowModal"
      :form-id="formId"
      :form-data="currentFormValue"
    />
  </div>
</template>

<script>
import { helper, request } from '@/utils/T';
import { GetFormData, SaveFormData, SendFlow } from '@/api/formManagement';
import SendFlowModal from './SendFlowModal';
export default {
  name: 'EditForm',
  components: { SendFlowModal },
  data() {
    return {
      edit: false,
      loading: false,
      isShowModal: false,
      formId: null,
      formConfig: {
        config: {},
        list: []
      },
      formValue: {},
      currentFormValue: {}
    };
  },
  mounted() {
    this.getFormData();
  },
  methods: {
    getFormData() {
      this.loading = true;
      const { workId, flowNo, edit } = helper.getUrlParams();
      GetFormData(workId, flowNo).then(res => {
        const { formData, formConfig } = res.data;
        this.formId = formConfig.id;
        this.formConfig = JSON.parse(formConfig.config);
        this.formValue = formData;
        this.loading = false;
        this.edit = !!edit;
      }, err => {
        this.$message.error(err.msg);
        this.loading = false;
      });
    },
    submit() {
      // this.loading = true;
      const { workId, user } = helper.getUrlParams();
      this.$refs['generateForm'].getData().then(data => {
        this.currentFormValue = data;
        this.isShowModal = true;
        // return SaveFormData({ workId, formId: this.formId, data });
      }, err => {
        this.$message.warning(err);
        // this.loading = false;
      });
      // .then(res => {
      //   return SendFlow({ userNo: user, sysNo: '1001', toEmps: 'test2', workId });
      // }, err => {
      //   this.loading = false;
      //   this.$message.error(err.msg);
      // }).then(res => {
      //   this.loading = false;
      //   this.$message.success(res.message);
      // }, err => {
      //   this.loading = false;
      //   this.$message.error(err.message);
      // });
    },
    formChange(field, value, data) {
      // console.log(field);
      // console.log(value);
      // console.log(data);
    },
    reset() {
      this.$refs['generateForm'].reset();
    },
    back(){
      this.$router.back();
    }
  }
};
</script>

<style scoped>

</style>
